package lyl.dcas;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

import com.ucap.dcas.common.bind.result.Result;


/**
 * @author Liu Yulong
 */
public class test {
    public static void main(String[] args) {
        Result result = new Result();
        result.setTitle("信息抓取测试");
        result.setStringContent("<html><H1>test success!</H1></html>");
        HashMap <String,String> map = new HashMap <String,String>();
        map.put("siteCode", "15");
        map.put("cateCode", "598");
        map.put("host", "192.168.1.195:8080");
        result.setAssitObj(map);
        HashMap <String,String> obj = (HashMap <String,String>) result.getAssitObj();
        HashMap <String,String> resultMap = new HashMap <String,String>();
        resultMap.put("title", result.getTitle());
        resultMap.put("content", result.getStringContent());
        resultMap.putAll(obj);
        String url = "http://" + obj.get("host") + "/dcas-cms-connect/servlet/DcasController";
        HttpClient client = new HttpClient();
        try {
            String jsonString = JSONSerializer.toJSON(resultMap).toString();
            System.out.println("==jsonString===" +jsonString);
            PostMethod filePost = new PostMethod(url);
            filePost.addParameter("type", "publish");
            filePost.addParameter("content",StringEscapeUtils.escapeJavaScript(new String(jsonString.getBytes("UTF-8"),"UTF-8")));
            HttpClientParams params = new HttpClientParams();
            params.setContentCharset("UTF-8");
            params.setHttpElementCharset("UTF-8");
            client.setParams(params);
            client.executeMethod(filePost);
            System.out.println("==response===" + filePost.getResponseBodyAsString());
            filePost.releaseConnection();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void getConnection(){
        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:microsoft:sqlserver://192.168.1.195:1433;databaseName=cmspro_yb", "cmspro", "cmspro");
            if (conn != null)
                System.out.println("Connection Successful!");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
